//
//  BCSvipMenberHeaderView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/20.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCSvipMenberHeaderView.h"

#import "BCUserSvipResponse.h"
//
#import "UICollectionViewLeftAlignedLayout.h"
#import "BCSvipMenberHeaderCollectionCell.h"

const CGFloat BCSvipMenberHeaderViewLeftPadding = 15.0;
const CGFloat BCSvipMenberHeaderViewPadding = 22.0;
const CGFloat BCSvipMenberHeaderViewTopPadding = 18.0;


@interface BCSvipMenberHeaderView () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout>

@property (nonatomic, strong) UICollectionView *menberCollection;

/**
 *menberSource
 */
@property (nonatomic, weak) NSArray *menberSource;

/**
 *noDataLabel
 */
@property (nonatomic, strong) QMUILabel *noDataLabel;

@end

@implementation BCSvipMenberHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
//        self.clipsToBounds = true;
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    //
    UICollectionViewLeftAlignedLayout *layout = [UICollectionViewLeftAlignedLayout new];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(BCSvipMenberHeaderViewTopPadding*HeightScale, BCSvipMenberHeaderViewLeftPadding*WidthScale, BCSvipMenberHeaderViewTopPadding*HeightScale, BCSvipMenberHeaderViewLeftPadding*WidthScale);
    layout.sectionInset = edgeInsets;
    //
    layout.minimumInteritemSpacing = BCSvipMenberHeaderViewPadding*WidthScale;
    layout.minimumLineSpacing = BCSvipMenberHeaderViewPadding*WidthScale;
    //
    self.menberCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.contentView addSubview:self.menberCollection];
    [self.menberCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    //
    [self.menberCollection registerClass:BCSvipMenberHeaderCollectionCell.class forCellWithReuseIdentifier:NSStringFromClass(BCSvipMenberHeaderCollectionCell.class)];
    self.menberCollection.dataSource = self;
    self.menberCollection.delegate = self;
    //
    self.menberCollection.backgroundColor = [UIColor clearColor];
    self.menberCollection.showsHorizontalScrollIndicator = false;
    //
    UIView *sepLine = [UIView new];
    sepLine.backgroundColor = [UIColor separatorColor];
    [self.contentView addSubview:sepLine];
    [sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(BCSvipMenberHeaderViewLeftPadding*WidthScale);
        make.right.mas_equalTo(self.contentView).offset(-BCSvipMenberHeaderViewLeftPadding*WidthScale);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(1);
    }];
    //
    self.noDataLabel = [QMUILabel new];
    self.noDataLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.noDataLabel.numberOfLines = 0;
    self.noDataLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.noDataLabel];
    self.noDataLabel.textAlignment = NSTextAlignmentCenter;
    [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_bottom).offset(20.0);
        make.centerX.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView).multipliedBy(0.7);
    }];
    self.noDataLabel.alpha = 0;
}

- (void)setSvipData:(BCUserSvipData *)svipData {
    _svipData = svipData;
    //
    self.menberSource = svipData.shares;
    //
    [self.menberCollection reloadData];
    
    if (svipData.currentShares&&!svipData.currentShares.content.count) {
        self.noDataLabel.alpha = 1;
        self.noDataLabel.text = svipData.flow.not_data;
    }else {
        self.noDataLabel.alpha = 0;
    }
}


//
#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.menberSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BCSvipMenberHeaderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(BCSvipMenberHeaderCollectionCell.class) forIndexPath:indexPath];
    cell.share = self.menberSource[indexPath.row];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = collectionView.frame.size.width;
    return [self.class cellSizeForWidth:width];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    BCUserSvipDataShares *itemData = self.menberSource[indexPath.row];
    if (itemData!=self.svipData.currentShares) {
        self.svipData.currentShares.selectFlag = false;
        itemData.selectFlag = true;
        self.svipData.currentShares = itemData;
        //
        [self.tableView reloadData];
    }
}



+ (CGSize)cellSizeForWidth:(CGFloat)width {
    //2个间隔 2个左右间隔 3个cell
    CGFloat paddings = 2*(BCSvipMenberHeaderViewPadding*WidthScale);
    CGFloat leftRightPaddings = 2*(BCSvipMenberHeaderViewLeftPadding*WidthScale);
    CGFloat cellWidth = (width-paddings-leftRightPaddings)/3.0;
    return CGSizeMake(cellWidth, cellWidth);
}

+ (CGFloat)heightWithWidth:(CGFloat)width {
    //需要根据宽度计算出3个视图的高度
    CGFloat height = 0;
    CGSize cellSize = [self cellSizeForWidth:width];
    height += (BCSvipMenberHeaderViewTopPadding*HeightScale);
    height += cellSize.height;
    height += (BCSvipMenberHeaderViewTopPadding*HeightScale);
    return height;
}

@end
